﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace CSKB
{
    [StructLayout(LayoutKind.Sequential)]
    public struct MouseInput
    {
        public Int32 dx;
        public Int32 dy;
        public Int32 Mousedata;
        public Int32 dwFlag;
        public Int32 time;
        public IntPtr dwExtraInfo;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct tagKEYBDINPUT
    {
       public Int16 wVk;
       public Int16 wScan;
       public Int32 dwFlags;
       public Int32 time;
       public IntPtr dwExtraInfo;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct tagHARDWAREINPUT
    {
        Int32 uMsg;
        Int16 wParamL;
        Int16 wParamH;
    }

    [StructLayout(LayoutKind.Explicit)]
    public struct Input
    {
        [FieldOffset(0)]
        public Int32 type;
        [FieldOffset(4)]
        public MouseInput mi;
        [FieldOffset(4)]
        public tagKEYBDINPUT ki;
        [FieldOffset(4)]
        public tagHARDWAREINPUT hi;
    } 

    public static class Win32API
    {
        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className,  string  windowTitle);

        [DllImport("user32.dll")]
        public static extern IntPtr GetParent(IntPtr hWnd);

        [DllImport("user32.dll")]
        public static extern int SendDlgItemMessage(IntPtr hDlg, int nIDDlgItem, uint Msg, int wParam, int lParam);

        [DllImport("user32.dll")]
        public static extern IntPtr GetDlgItem(IntPtr hWnd, int itemId);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool SetForegroundWindow(IntPtr hWnd);

        //[DllImport("user32.dll", EntryPoint = "SendMessageW")]
        [DllImport("user32.dll")]
        public static extern int SendMessage(
             IntPtr hwnd,
             int wMsg,
             int wParam,
             int lParam);

        //[DllImport("user32.dll", EntryPoint = "PostMessageW", SetLastError = true)]
        [DllImport("user32.dll")]
        public static extern int PostMessage(
             IntPtr hwnd,
             int wMsg,
             int wParam,
             int lParam);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr GetForegroundWindow();

        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr GetFocus();

        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr SetFocus(IntPtr hWnd);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern int AttachThreadInput(
             int idAttach,
             int idAttachTo,
             bool fAttach);

        [DllImport("user32.dll")]
        public static extern int GetWindowThreadProcessId(
             IntPtr hwnd,
             out int lpdwProcessId);

      //  [DllImport("user32.dll")]
     //   public static extern bool SetWindowText(
    //         IntPtr hwnd,
     //        string str);
        

        [DllImport("kernel32.dll")]
        public static extern int GetCurrentThreadId();

        [DllImport("kernel32.dll")]
        public static extern int GetCurrentProcessId();

        [DllImport("user32.dll")]
        public static extern int MessageBox(IntPtr hWnd, string text, string caption, uint type);
        
        //[DllImport("user32.dll")]
        //public static extern IntPtr GetTopWindow(IntPtr hWnd);
     //   [DllImport("user32.dll")]
      //  public static extern UInt32 SendInput(UInt32 nInputs, Input[] pInputs, int cbSize);
        
        public const int WM_MOUSEACTIVATE = 0x21;
        public const int WM_KEYDOWN = 0x100;
        public const int MA_NOACTIVATE = 3;
        public const int WS_EX_NOACTIVATE = 0x8000000;
        public const int WM_IME_CHAR = 0x286;
        public const int WM_IME_KEYDOWN                =  0x0290;
        public const int WM_IME_KEYUP                =    0x0291;
        public const int WM_KEYUP                      =  0x0101;
        public const int WM_CHAR = 0x0102;
        public const int WM_PASTE                       = 0x0302;
        public const int WM_COMMAND                     = 0x0111;

        public const int INPUT_KEYBOARD = 1;
        public const int KEYEVENTF_KEYUP = 0x0002;
        public const int WM_LBUTTONDOWN   = 0x0201;
        public const int WM_LBUTTONUP = 0x0202;
        //public const int EN_CHANGE = 0x03000000;
        //public const int EN_UPDAT = 0x04000000;
        public const int EM_SETSEL = 0x00B1;
    }


}
